package cn.e4j.modular.crm.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.common.exception.BizExceptionEnum;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.InvokeUtil;
import cn.e4j.modular.common.service.SysAuthorityService;
import cn.e4j.modular.crm.entity.GwgcT;
import cn.e4j.modular.crm.entity.GwgdT;
import cn.e4j.modular.crm.entity.GwgeT;
import cn.e4j.modular.crm.service.GwgcTService;
import cn.e4j.modular.system.mapper.SysOrgMapper;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 投标信息申请
 * Created by miaoyl on 2019-1-11.
 */
@Controller
@RequestMapping("/crm/gwgi002")
public class Gwgi002Controller extends BaseController {
    @Autowired
    private GwgcTService gwgcTService;

    @Autowired
    SysOrgMapper sysOrgMapper;

    /**
     * 跳转投标信息申请管理页面
     */
    @RequiresPermissions("crm:gwgi002:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwgi002.html";
    }

    /**
     * 查询投标信息申请列表
     */
    @RequestMapping("/getGwgclist")
    @RequiresPermissions("crm:gwgi002:init")
    @ResponseBody
    public ResponseData getGwgclist(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        String ORG_ID = shiroUser.getORG_ID();
//        if(sysAuthorityService.getNolimitOrg(ORG_ID,null) == null ){
//            //客户权限
//            inDto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(inDto));
//            //部门权限
//            List<Dto> orgs = sysAuthorityService.getOrgsAuthority(inDto);
//            if(orgs.size() > 0) {
//                orgs.removeIf(orgDto -> orgDto.getString("ID").equals(ORG_ID));
//            }
//            inDto.put("CURR_USER",shiroUser.getID());
//            inDto.put("ORGS", orgs);
//        }

        List<Map<String, Object>> result = InvokeUtil.invokeBeanMethod( "gwgcTMapper,getGwgcList", page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 获取新增默认值
     */
    @RequestMapping("/getAddData")
    @ResponseBody
    public ResponseData getAddDetailData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto addDataDto = gwgcTService.getAddData(inDto);
        return new SuccessResponseData(addDataDto);
    }

    /**
     * 新增
     */
    @RequestMapping("/addData")
    @RequiresPermissions("crm:gwgi002:add")
    @ResponseBody
    public ResponseData addData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwgdT> gwgdTList = JSONObject.parseArray(jsonObject.getString("gwgdList"), GwgdT.class);
        GwgcT gwgcT = JSONObject.parseObject(jsonObject.getString("gwgcData"), GwgcT.class);
        if (gwgcTService.existedTheParams(gwgcT)) {
            return new ErrorResponseData(BizExceptionEnum.EXISTED_THE_PARAMS.getCode(),
                    "招标编号重复");
        }
        gwgcTService.addData(gwgdTList, gwgcT);
        return SUCCESS_TIP;
    }

    /**
     * 修改
     */
    @RequestMapping("/editData")
    @RequiresPermissions("crm:gwgi002:edit")
    @ResponseBody
    public ResponseData editData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwgdT> gwgdTList = JSONObject.parseArray(jsonObject.getString("gwgdList"), GwgdT.class);
        GwgcT gwgcT = JSONObject.parseObject(jsonObject.getString("gwgcData"), GwgcT.class);
        gwgcTService.editData(gwgdTList, gwgcT);
        return SUCCESS_TIP;
    }

    /**
     * 删除
     */
    @RequestMapping("/del")
    @RequiresPermissions("crm:gwgi002:del")
    @ResponseBody
    public ResponseData delete(GwgcT gwgcT) {
        gwgcTService.deleteData(gwgcT);
        return SUCCESS_TIP;
    }

    /**
     * 批量删除数据
     */
    @RequestMapping("/delDataBatch")
    @RequiresPermissions("crm:gwgi002:del")
    @ResponseBody
    public ResponseData deleteDataBatch(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwgcT> gwgcTList = inDto.getReqList(GwgcT.class);
        gwgcTService.delDataBatch(gwgcTList);
        return SUCCESS_TIP;
    }

    /**
     * 根据招标编号查询GWGD、GWGE数据
     */
    @RequestMapping("/getTable")
    @ResponseBody
    public ResponseData getTable(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = new Dto();
        List<Map<String, Object>> gwgdList = this.gwgcTService.getGwgdData(inDto);
        List<Map<String, Object>> gwgeList = this.gwgcTService.getGwgeData(inDto);
        outDto.put("gwgdList",gwgdList);
        outDto.put("gwgeList",gwgeList);
        return ResponseData.success(outDto);
    }

    /**
     * 新增中标信息
     */
    @RequestMapping("/addGwgeData")
    @ResponseBody
    public ResponseData addGwgeData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwgdT> gwgdTList = JSONObject.parseArray(jsonObject.getString("gwgdList"), GwgdT.class);
        List<GwgeT> gwgeTList = JSONObject.parseArray(jsonObject.getString("gwgeList"), GwgeT.class);
        GwgcT gwgcT = JSONObject.parseObject(jsonObject.getString("gwgcData"), GwgcT.class);
        gwgcTService.addGwgeData(gwgdTList, gwgeTList, gwgcT);
        return SUCCESS_TIP;
    }

    /**
     * 修改状态
     */
    @RequestMapping("/updateStatus")
    @RequiresPermissions("crm:gwgi002:init")
    @ResponseBody
    public ResponseData updateStatus(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<GwgcT> gwgcTList = inDto.getReqList(GwgcT.class);
        Dto outDto = gwgcTService.updateStatus(gwgcTList);
        if(outDto.getAppCode() == 500) {
            return new ErrorResponseData(500,outDto.getAppMsg());
        }
        return SUCCESS_TIP;
    }

    /**
     * 查询投标车型信息
     */
    @RequestMapping("/getGwgdList")
    @RequiresPermissions("crm:gwgi002:init")
    @ResponseBody
    public ResponseData getGwgdList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = InvokeUtil.invokeBeanMethod( "gwgcTMapper,getGwgdList", page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 查询投标反馈明细信息
     */
    @RequestMapping("/getGwgeList")
    @RequiresPermissions("crm:gwgi002:init")
    @ResponseBody
    public ResponseData getGwgeList(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = this.gwgcTService.getGwgeList(page, inDto);
        return new PageReData(page, result);
    }

    /**
     * 根据招标编号GWGE笔数
     */
    @RequestMapping("/getGwgeCount")
    @ResponseBody
    public ResponseData getGwgeCount(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Dto outDto = this.gwgcTService.getGwgeCount(inDto);
        return ResponseData.success(outDto);
    }

//    /**
//     * 查看审批流程
//     */
//    @RequestMapping("/checkApprovalProcess")
//    @ResponseBody
//    public ResponseData checkApprovalProcess(@DtoParam DtoWapper dtoWapper) {
//        Dto inDto = dtoWapper.getDto();
//        List<Dto> outDtoList = this.oaCheckApprovalProcess.getList(inDto);
//        return ResponseData.success(outDtoList);
//    }

    /**
     * 查询投标信息申请列表
     */
    @RequestMapping("/getGwgcData")
    @ResponseBody
    public ResponseData getGwgcData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = InvokeUtil.invokeBeanMethod( "gwgcTMapper,getGwgcData", page, inDto);
        return new PageReData(page, result);
    }
}
